package com.mjk.common.web.getway.restful.interceptor.type.commons.impl;


import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.tools.interceptor.InterceptorChain;
import com.mjk.common.tools.logger.BizLogger;
import com.mjk.common.tools.springextend.BeeApplicationListener;
import com.mjk.common.util.LicenseUtils;
import com.mjk.common.util.MapUtils;
import com.mjk.common.web.getway.constns.GetWayError;
import com.mjk.common.web.getway.constns.GetwayConsts;
import com.mjk.common.web.getway.restful.interceptor.GetWayInterceptorReq;
import com.mjk.common.web.getway.restful.interceptor.annotation.BeeGetWayInterceptor;
import com.mjk.common.web.getway.restful.interceptor.type.GetWayInterceptor;
import com.mjk.common.web.getway.restful.interceptor.type.GetWayInterceptorType;
import org.apache.commons.io.IOUtils;
import org.springframework.context.event.ContextRefreshedEvent;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * license 效应拦截连
 *  license 文件打包之后放入 bin目录下 如果是工程中，放入工程同级目录即可
 * @author msi
 * <a href="http://localhost:8072/ld/common/v1/license?reqCode=">...</a>***
 */
//@BeeGetWayInterceptor(value = 11, interceptorType = {GetWayInterceptorType.COMMON})
public class GetWayLicenseAuthInterceptor implements GetWayInterceptor {

	@Override
	public BeeResponse interceptor(GetWayInterceptorReq request,
								   InterceptorChain<GetWayInterceptorReq, BeeResponse> next) {
			//判断是否是license申请 请求
			if("license".equals(request.getRestfulParams().get(GetwayConsts.INSTANCE))){
				String reqCode = MapUtils.getString(request.getParams(), "reqCode");
                if (LicenseUtils.generateLicense(reqCode)) {
                    return GetWayError.LICENSE_SUC.tranceError("");
                } else {
                    return GetWayError.LICENSE_ERROR.tranceError("");
                }
            } else {
                if (!LicenseUtils.checkLicense()) {
                    BeeResponse reponse = GetWayError.LICENSE_ERROR.tranceError("License申请码为:" + LicenseUtils.getReqCode());
					reponse.setMsg(reponse.getData().toString());
					return reponse;
                }
            }

		return next.proceed(request);
	}


}
